package cn.tlb.gl.game.rebuild2;

import android.graphics.Bitmap;
import android.opengl.GLUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.microedition.khronos.opengles.GL10;

/**
 * <p>64x32皮肤的切割工具(获取纹理)</p>
 *
 * Created by 汤林冰 on 2016/2/1 09:33.
 */
public class SkinCrop64x32 implements SkinMappingIndex {

    public  static final int FIXED_WIDTH = 64;
    public  static final int FIXED_HEIGHT = 32;
    private static final int unit = FIXED_WIDTH/8;
    private static final int half_unit = unit/2;
    private static final int tow_third_unit = 3*unit/2;

    private Bitmap src;
    private LinkedHashMap<Integer, Bitmap> map = new LinkedHashMap<Integer, Bitmap>();

    public SkinCrop64x32(Bitmap src) throws BitmapSizeUnexpectedException {
        if(src.getHeight() == FIXED_HEIGHT && src.getWidth() == FIXED_WIDTH) {
            this.src = src;
            initMap();
            src.recycle();
            return;
        }
        throw new BitmapSizeUnexpectedException(src);
    }

    private void initMap() {
        map.put(index_head_top   , Bitmap.createBitmap(src, unit,   0,    unit, unit));
        map.put(index_head_front , Bitmap.createBitmap(src, unit,   unit, unit, unit));
        map.put(index_head_left  , Bitmap.createBitmap(src, 2*unit, unit, unit, unit));
        map.put(index_head_right , Bitmap.createBitmap(src, 0,      unit, unit, unit));
        map.put(index_head_back  , Bitmap.createBitmap(src, 3*unit, unit, unit, unit));
        map.put(index_head_bottom, Bitmap.createBitmap(src, 2*unit, 0,    unit, unit));
        // translate head vector (4*unit,0)
        map.put(index_cap_top   , Bitmap.createBitmap(src, 4*unit+unit,   0,    unit, unit));
        map.put(index_cap_front , Bitmap.createBitmap(src, 4*unit+unit,   unit, unit, unit));
        map.put(index_cap_left  , Bitmap.createBitmap(src, 4*unit+2*unit, unit, unit, unit));
        map.put(index_cap_right , Bitmap.createBitmap(src, 4*unit+0,      unit, unit, unit));
        map.put(index_cap_back  , Bitmap.createBitmap(src, 4*unit+3*unit, unit, unit, unit));
        map.put(index_cap_bottom, Bitmap.createBitmap(src, 4*unit+2*unit, 0,    unit, unit));

        map.put(index_left_leg_top   , Bitmap.createBitmap(src, half_unit,  2*unit, half_unit, half_unit));
        map.put(index_left_leg_front , Bitmap.createBitmap(src, half_unit,  2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_leg_left  , Bitmap.createBitmap(src, unit,       2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_leg_right , Bitmap.createBitmap(src, 0,          2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_leg_back  , Bitmap.createBitmap(src, unit+half_unit, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_leg_bottom, Bitmap.createBitmap(src, unit, 2 * unit, half_unit, half_unit));

        map.put(index_body_top   , Bitmap.createBitmap(src, 2*unit+half_unit, 2*unit, unit, half_unit));
        map.put(index_body_front , Bitmap.createBitmap(src, 2*unit+half_unit, 2*unit+half_unit, unit, tow_third_unit));
        map.put(index_body_left  , Bitmap.createBitmap(src, 2*unit+tow_third_unit, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_body_right , Bitmap.createBitmap(src, 2*unit+0, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_body_back  , Bitmap.createBitmap(src, 2*unit+2*unit, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_body_bottom, Bitmap.createBitmap(src, 2*unit+tow_third_unit, 2*unit, unit, half_unit));

        // translate leg vector (5*unit,0)
        map.put(index_left_arm_top   , Bitmap.createBitmap(src, 5*unit+half_unit, 2*unit, half_unit, half_unit));
        map.put(index_left_arm_front , Bitmap.createBitmap(src, 5*unit+half_unit, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_arm_left  , Bitmap.createBitmap(src, 5*unit+unit, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_arm_right , Bitmap.createBitmap(src, 5*unit+0, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_arm_back  , Bitmap.createBitmap(src, 5*unit+unit+half_unit, 2*unit+half_unit, half_unit, tow_third_unit));
        map.put(index_left_arm_bottom, Bitmap.createBitmap(src, 5*unit+unit, 2*unit, half_unit, half_unit));
    }

    public int[] genTextures(GL10 gl){
        int textures[] = new int[map.size()];
        gl.glGenTextures(textures.length, textures, 0);

        for(Map.Entry<Integer, Bitmap> entry : map.entrySet()){
            Bitmap bitmap = entry.getValue();
            gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[entry.getKey()]);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
            //放大插值就近取像素
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
            gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
            GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
            bitmap.recycle();
        }

        return textures;
    }
}











































